
package cn.iocoder.lw.module.member.api.user;

import cn.iocoder.lw.module.member.api.user.dto.AdminUserSyncDTO;
import cn.iocoder.lw.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.lw.module.member.convert.user.MemberUserConvert;
import cn.iocoder.lw.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.lw.module.member.service.user.MemberUserService;
import cn.iocoder.lw.module.member.enums.user.MemberUserTypeEnum;
import cn.iocoder.lw.framework.common.enums.TerminalEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import jakarta.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.lw.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.lw.module.member.enums.ErrorCodeConstants.USER_MOBILE_NOT_EXISTS;

/**
 * 会员用户的 API 实现类
 *
 * @author Lw源码
 */
@Service
@Validated
@Slf4j
public class MemberUserApiImpl implements MemberUserApi {

	@Resource
	private MemberUserService userService;

	@Override
	public MemberUserRespDTO getUser(Long id) {
		MemberUserDO user = userService.getUser(id);
		return MemberUserConvert.INSTANCE.convert2(user);
	}

	@Override
	public List<MemberUserRespDTO> getUserList(Collection<Long> ids) {
		return MemberUserConvert.INSTANCE.convertList2(userService.getUserList(ids));
	}

	@Override
	public List<MemberUserRespDTO> getUserListByNickname(String nickname) {
		return MemberUserConvert.INSTANCE.convertList2(userService.getUserListByNickname(nickname));
	}

	@Override
	public MemberUserRespDTO getUserByMobile(String mobile) {
		return MemberUserConvert.INSTANCE.convert2(userService.getUserByMobile(mobile));
	}

	@Override
	public void validateUser(Long id) {
		MemberUserDO user = userService.getUser(id);
		if (user == null) {
			throw exception(USER_MOBILE_NOT_EXISTS);
		}
	}

	@Override
	public Long createUserFromSystem(AdminUserSyncDTO adminUser) {
		try {
			log.info("开始从系统用户创建member用户，systemUserId: {}", adminUser.getId());

			// 检查是否已经存在该手机号的用户
			MemberUserDO existingUser = userService.getUserByMobile(adminUser.getMobile());
			if (existingUser != null) {
				// 如果已存在，更新用户类型和系统用户ID
				if (existingUser.getUserType() == null
						|| !existingUser.getUserType().equals(MemberUserTypeEnum.SERVICE_PROVIDER.getType())) {
					// 创建更新VO
					// 注意：这里需要根据实际的更新方法来实现
					log.info("手机号已存在，更新用户类型为服务商，memberUserId: {}, systemUserId: {}", existingUser.getId(),
							adminUser.getId());
				}
				return existingUser.getId();
			}

			// 创建新的member用户
			MemberUserDO memberUser = userService.createUserIfAbsent(adminUser.getMobile(), "127.0.0.1", // 默认IP，实际可以从上下文获取
					TerminalEnum.H5.getTerminal());

			if (memberUser != null) {
				// 更新用户信息
				String nickname = adminUser.getNickname() != null ? adminUser.getNickname() : adminUser.getUsername();
				// TODO: 这里需要调用updateUser方法来更新用户的其他信息
				// 目前先返回用户ID
				log.info("成功创建服务商用户，systemUserId: {}, memberUserId: {}", adminUser.getId(), memberUser.getId());
				return memberUser.getId();
			}

			return null;

		} catch (Exception e) {
			log.error("从系统用户创建member用户失败，systemUserId: {}", adminUser.getId(), e);
			throw e;
		}
	}

}
